﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;

namespace carshop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlCommand com;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommand com1;
        DataRow dr;

        private void Form1_Load(object sender, EventArgs e)
        {
            //for (int i = 0; i < Class1.xingbei.Length; i++)
            //{ comboBox1.Items.Add(Class1.xingbei[i]); }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = Class1.sqlstring;
            com1= new SqlCommand ();
            com1.CommandText ="select * from 会员信息";
            com1 .Connection =con;
            com = new SqlCommand();
            com.CommandText ="insert into 会员信息(姓名, 性别, 单位, 地址, 联系, 邮箱, 会员编号, 入会时间, 车主相片, 有效期, 车牌, 品牌, 车系, 车型, 上牌时间, 有效性, 汽车相片,身份证)"+
            "values(@姓名, @性别, @单位, @地址, @联系, @邮箱, @会员编号, @入会时间, @车主相片, @有效期, @车牌, @品牌, @车系, @车型, @上牌时间, @有效性, @汽车相片,@身份证)";
            com.Parameters.Add("@姓名", SqlDbType.NChar, 10, "姓名");
            com.Parameters.Add("@性别", SqlDbType.NChar, 10, "性别");
            com.Parameters.Add("@单位", SqlDbType.NChar, 10, "单位");
            com.Parameters.Add("@地址", SqlDbType.NChar, 10, "地址");
            com.Parameters.Add("@联系", SqlDbType.NChar, 10, "联系");
            com.Parameters.Add("@邮箱", SqlDbType.NChar, 10, "邮箱");
            com.Parameters.Add("@会员编号", SqlDbType.NChar, 10, "会员编号");
            com.Parameters.Add("@入会时间", SqlDbType.DateTime , 10, "入会时间");
            FileStream fs1 = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            Byte[] by1 = new Byte[fs1.Length];
            fs1.Read(by1, 0, by1.Length);
            fs1.Close();
            com.Parameters.Add("@车主相片", SqlDbType.Image ,by1 .Length , "车主相片");
            com.Parameters.Add("@有效期", SqlDbType.DateTime , 10, "有效期");
            com.Parameters.Add("@车牌", SqlDbType.NChar, 10, "车牌");
            com.Parameters.Add("@品牌", SqlDbType.NChar, 10, "品牌");
            com.Parameters.Add("@车系", SqlDbType.NChar, 10, "车系");
            com.Parameters.Add("@车型", SqlDbType.NChar, 10, "车型");
            com.Parameters.Add("@上牌时间", SqlDbType.DateTime , 10, "上牌时间");
            com.Parameters.Add("@有效性", SqlDbType.NChar, 10, "有效性");
            com.Parameters.Add("@身份证", SqlDbType.NChar, 10, "身份证");
            FileStream fs2 = new FileStream(openFileDialog2.FileName, FileMode.Open, FileAccess.Read);
            Byte[] by2 = new Byte[fs2.Length];
            fs2.Read(by2, 0, by2.Length);
            fs2.Close();
            com.Parameters.Add("@汽车相片", SqlDbType.Image ,by2 .Length , "汽车相片");
            com.Connection = con;
            //com1 .Parameters .AddWithValue ("",
            da = new SqlDataAdapter();
            da.SelectCommand = com1;
            da.InsertCommand = com;
            ds = new DataSet();
            da.Fill(ds, "会员信息");
            dr = ds.Tables["会员信息"].NewRow();
            dr["姓名"] = textBox1.Text;
            dr["性别"] = comboBox1.Text;
            dr["单位"] = textBox5.Text;
            dr["地址"] = textBox4.Text;
            dr["联系"] = textBox2.Text;
            dr["邮箱"] = textBox6.Text;
            dr["会员编号"] = textBox3.Text;
            dr["入会时间"] = dateTimePicker2 .Text ;
            dr["有效期"] = dateTimePicker3 .Text ;
            dr["车主相片"] = by1 ;
            dr["车牌"] = textBox9.Text;
            dr["品牌"] = comboBox4 .Text ;
            dr["车系"] = comboBox2 .Text ;
            dr["车型"] = comboBox3.Text;
            dr["上牌时间"] = dateTimePicker4.Text;
            dr["有效性"] ="1";
            dr["汽车相片"] = by2 ;
            dr["身份证"] =textBox7 .Text ;
            ds.Tables["会员信息"].Rows.Add(dr);
            da.Update(ds.Tables["会员信息"]);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.FileName = "";
                openFileDialog1.ShowDialog();
                if (openFileDialog1.FileName != "")
                {
                    pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载照片有误" + ex.Message.ToString());
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog2.FileName = "";
                openFileDialog2.ShowDialog();
                if (openFileDialog1.FileName != "")
                {
                    pictureBox2.Image = Image.FromFile(openFileDialog2.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载照片有误" + ex.Message.ToString());
            }
        }


    }
}